$(function () {
    console.info("start role");

    let token = $("meta[name='_csrf']").attr("content");
    let header = $("meta[name='_csrf_header']").attr("content");
    $(document).ajaxSend(function (e, xhr, options) {
        xhr.setRequestHeader(header, token);
    });

    /*$('#dataTable').bootstrapTable("refreshOptions", {
        columns: [{
            title: '行号',
            align: 'center',
            valign: 'bottom',
            formatter: function (value, row, index) {
                return index + 1;
            }
        }]
    });*/
});

function doCreate(obj) {
    $('#roleForm')[0].reset();
    $('#roleModal').modal('show');
}

function doEdit(obj) {
    $('#roleForm')[0].reset();
    let rows = $('#dataTable').bootstrapTable('getSelections');
    if (rows.length == 1) {
        $('#roleForm').setFormValue(rows[0]);
        $('#roleModal').modal('show');
    } else {
        $('#_errorMes').val("请选择一条记录！");
        $('#_error').toast('show');
    }
}

function doDelete(obj) {
    let rows = $('#dataTable').bootstrapTable('getSelections');
    if (rows.length > 0) {
        let rows = $('#dataTable').bootstrapTable('getSelections');
        $.ajax({
            url: "/role/deleteInBatch",
            type: "DELETE",
            contentType: "application/json",
            dataType: "json",
            data: JSON.stringify(rows),
            success: function (result) {
                $('#roleModal').modal('hide');
                $('#_successMes').val("删除成功！");
                $('#_success').toast('show');
                $('#dataTable').bootstrapTable('refresh');
            },
            error: function (e) {
                $('#roleModal').modal('hide');
                $('#_errorMes').val("数据库错误：" + e.responseText);
                $('#_error').toast('show');
            }
        });
    } else {
        $('#_errorMes').val("请至少选择一条记录！");
        $('#_error').toast('show');
    }
}

function doSave(obj) {
    let data = $('#roleForm').serializeObject();
    $.ajax({
        url: "/role/save",
        type: "POST",
        contentType: "application/json",
        dataType: "json",
        data: JSON.stringify(data),
        success: function (result) {
            $('#roleModal').modal('hide');
            $('#_successMes').val("保存成功！");
            $('#_success').toast('show');
            $('#dataTable').bootstrapTable('refresh');
            /*if (data.id == '') {
                $('#dataTable').bootstrapTable('insertRow', {
                    index: 0,
                    row: result
                })
            } else {
                $('#dataTable').bootstrapTable('updateByUniqueId', {
                    id: result.id,
                    row: result
                })
            }*/
        },
        error: function (e) {
            $('#roleModal').modal('hide');
            $('#_errorMes').val("数据库错误：" + e.responseText);
            $('#_error').toast('show');
        }
    });
}